﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using TpBaseObjects.Models.BusinessCases;
using TpBaseObjects.Objects;
using TpData.Layer;

namespace TpData.Manager {

    public class BusinessCaseDataManager {

        #region Load

        public static int GetPurchaserNamesCount(int purchaserID, int year) {
            using (BusinessCaseDataLayer dl = CreateBusinessCaseDataLayer()) {
                return dl.GetPurchaserNamesCount(purchaserID, year);
            }
        }

        public static int GetPurchaserProjectCount(int purchaserID, int year) {
            using (BusinessCaseDataLayer dl = CreateBusinessCaseDataLayer()) {
                return dl.GetPurchaserProjectCount(purchaserID, year);
            }
        }

        public static BusinessCaseModel LoadBusinessCase(int projectID) {
            BusinessCaseModel ret;
            using (BusinessCaseDataLayer dl = CreateBusinessCaseDataLayer()) {
                ret = dl.LoadBusinessCase(projectID);
            }
            if (ret == null) {
                ret = new BusinessCaseModel();
            }
            return ret;
        }

        public static List<BusinessCasesShortListModel> LoadBusinessCasesShortList(BusinessCasesSelectModel selectModel) {

            SqlParameter[] parameters = GetParameters(selectModel);

            using (BusinessCaseDataLayer dl = CreateBusinessCaseDataLayer()) {
                return dl.LoadBusinessCasesShortList(parameters);
            }
        }

        public static BusinessCaseStatusObj LoadBusinessCaseStatus(byte status) {
            using (BusinessCaseDataLayer dl = CreateBusinessCaseDataLayer()) {
                return dl.LoadBusinessCaseStatus(status);
            }
        }

        public static List<BusinessCaseStatusObj> LoadBusinessCaseStatusList() {
            return LoadBusinessCaseStatusList(false);
        }

        public static List<BusinessCaseStatusObj> LoadBusinessCaseStatusList(bool withNull) {
            List<BusinessCaseStatusObj> ret;
            using (BusinessCaseDataLayer dl = CreateBusinessCaseDataLayer()) {
                ret = dl.LoadBusinessCaseStatusList();
            }
            if (withNull && ret != null) {
                BusinessCaseStatusObj bs = new BusinessCaseStatusObj();
                bs.ProjectStatus = 0;
                bs.StateText = "";
                bs.FilePath = "";
                ret.Add(bs);
                ret.Sort();
            }
            return ret;
        }

        public static List<CustomerSourceObj> LoadCustomerSourceList() {
            return LoadCustomerSourceList(false);
        }

        public static List<CustomerSourceObj> LoadCustomerSourceList(bool withNull) {
            List<CustomerSourceObj> ret;
            using (BusinessCaseDataLayer dl = CreateBusinessCaseDataLayer()) {
                ret = dl.LoadCustomerSourceList();
            }
            if (withNull && ret != null) {
                CustomerSourceObj cs = new CustomerSourceObj();
                cs.CustomerSourceID = 0;
                cs.CustomerSourceLong = "";
                cs.CustomerSourceShort = "";
                ret.Add(cs);
                ret.Sort();
            }
            return ret;
        }

        public static CustomerSourceObj LoadCustomerSource(int customerSourceID) {
            using (BusinessCaseDataLayer dl = CreateBusinessCaseDataLayer()) {
                return dl.LoadCustomerSource(customerSourceID);
            }
        }

        #endregion

        #region Save

        public static void SaveBusinessCase(BusinessCaseModel model) {
            using (BusinessCaseDataLayer dl = CreateBusinessCaseDataLayer()) {
                dl.SaveBusinessCase(model);
                model.Saved = true;
            }
        }

        #endregion

        #region Update

        public static void UpdateBusinessCase(BusinessCaseModel model) {
            using (BusinessCaseDataLayer dl = CreateBusinessCaseDataLayer()) {
                dl.UpdateBusinessCase(model);
            }
        }

        #endregion

        #region Functions

        private static BusinessCaseDataLayer CreateBusinessCaseDataLayer() {
            return (DataManager.CreateDataLayer(typeof(BusinessCaseDataLayer)) as BusinessCaseDataLayer);
        }

        private static SqlParameter[] GetParameters(BusinessCasesSelectModel selectModel) {
            byte i = 0;
            SqlParameter[] parameters = new SqlParameter[10];

            parameters[i] = new SqlParameter("@CreatedFrom", selectModel.CreatedFrom);
            i += 1;
            parameters[i] = new SqlParameter("@CreatedTo", selectModel.CreatedTo);
            i += 1;
            if (selectModel.CustomerGroup != 0) {
                parameters[0] = new SqlParameter("@CustomerGroup", selectModel.CustomerGroup);
                i += 1;
            }
            if (selectModel.CustomerSource != 0) {
                parameters[i] = new SqlParameter("@CustomerSource", selectModel.CustomerSource);
                i += 1;
            }
            if (selectModel.FileUserID != 0) {
                parameters[i] = new SqlParameter("@FileUser", selectModel.FileUserID);
                i += 1;
            }
            if (selectModel.Intern) {
                parameters[i] = new SqlParameter("@Intern", selectModel.Intern);
                i += 1;
            }
            if (selectModel.ProjectAddress != null && selectModel.ProjectAddress != "") {
                parameters[i] = new SqlParameter("@ProjectStrasse", selectModel.ProjectAddress);
                i += 1;
            }
            if (selectModel.ProjectEPLZ != 0) {
                parameters[i] = new SqlParameter("@EPLZ", selectModel.ProjectEPLZ);
                i += 1;
            }
            if (selectModel.ProjectStatus != 0) {
                parameters[i] = new SqlParameter("@ProjectStatus", selectModel.ProjectStatus);
                i += 1;
            }
            if (selectModel.Purchaser != 0) {
                parameters[i] = new SqlParameter("@Purchaser", selectModel.Purchaser);
                i += 1;
            }

            return parameters;
        }

        #endregion

    }
}
